package com.oocl.webmvc.controller;

import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.webmvc.factory.ServiceFactory;
import com.oocl.webmvc.log4j.Log4j;
import com.oocl.webmvc.pojo.Customer;
import com.oocl.webmvc.service.CustomerService;

/**
 * @description: 该controller 为单例模式，只初始化一次
 * @author  XIEMI2
 * @date Aug 7, 2017 4:22:31 PM
 * @version 1.0
 */
@Controller
public class CustomerController {

	private CustomerService customerService;
	
	public CustomerController() {  
		customerService = ServiceFactory.getCustomerService();
	}

	@ResponseBody
	@RequestMapping("/addCustomer")
	public Customer addCustomer(@RequestBody Customer cust){
		
		cust.setId(UUID.randomUUID().toString());
		System.out.println("cust..."+cust.toString());
		customerService.addCustomer(cust);
		return cust;  
	}
	
	@RequestMapping("/delCustomer")
	public void delCustomer(String customerId){
		System.out.println("当前需要删除的Id为 "+customerId);
		customerService.delCustomer(customerId);
		System.out.println("删除后还剩下的customers 信息为： "+customerService.findAllCustomers());
	}
	
	@ResponseBody
	@RequestMapping("/updateCustomer")
	public List<Customer> updateCustomer(@RequestBody Customer cust){
		return customerService.updateCustomer(cust);
	}
	
	@RequestMapping("/showAddCustomerPage")
	public String showAddCustomerPage(){
		return "addCustomer";
	}
	
	@ResponseBody
	@RequestMapping("/loadAllCustomers")
	public List<Customer> loadAllCustomers(){
		System.out.println("进来 loadAll");
		System.out.println("当前有的： "+customerService.findAllCustomers());
		return customerService.findAllCustomers();
	}
}


